package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.itheima.entity.Recommend;
import com.itheima.mapper.RecommendMapper;
import com.itheima.service.RecommendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.CachingRecommender;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author guanhuaer
 * @since 2024-09-17
 */
@Service
public class RecommendServiceImpl extends ServiceImpl<RecommendMapper, Recommend> implements RecommendService {

    @Autowired
    private RecommendMapper recommendMapper;

    @Override
    public List recomend(Integer id) throws TasteException {
        List<RecommendedItem> recommends = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            MysqlDataSource dataSource = new MysqlDataSource();
            dataSource.setServerName("localhost");
            dataSource.setPort(3306);
            dataSource.setUser("root");
            dataSource.setPassword("root");
            dataSource.setDatabaseName("innaems");
            DataModel dataModel = new MySQLJDBCDataModel(dataSource,"recommend",
                    "user_id","project_id","score",
                    "create_time");
            UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
            double similarity1 = similarity.userSimilarity(1L,1L);
            double similarity2 = similarity.userSimilarity(1L,2L);
            double similarity3 = similarity.userSimilarity(1L,3L);
            double similarity4 = similarity.userSimilarity(1L,4L);
            double similarity5 = similarity.userSimilarity(1L,5L);
            double similarity6 = similarity.userSimilarity(1L,6L);
            UserNeighborhood neighborhood = new NearestNUserNeighborhood(5,similarity,dataModel);//计算邻居
            long[] userNeighborhood = neighborhood.getUserNeighborhood(id);//获取当前用户的邻居
            System.out.println("当前用户的邻居为："+ JSON.toJSONString(userNeighborhood));
            Recommender recommender = new CachingRecommender(new GenericUserBasedRecommender(dataModel,neighborhood,similarity));//推荐
            recommends = recommender.recommend(id,8);//userId,size
            System.out.println(JSON.toJSONString(recommends));
            return recommends;

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }


    }
}
